//
//  ANOrderInfoController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANOrderInfoController.h"
#import "MineAddressView.h"
#import "OrderCartView.h"
#import "OrderInfoView.h"
#import "OrderInfoActionView.h"
#import "ANOrderPayController.h"

@interface ANOrderInfoController ()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIImageView *orderStateBgView;
@property (nonatomic, strong) UILabel *orderStateLab;
@property (nonatomic, strong) MineAddressView *addressView;
@property (nonatomic, strong) OrderCartView *orderCartView;
@property (nonatomic, strong) OrderInfoView *orderInfoView;
@property (nonatomic, strong) OrderInfoActionView *actionView;

@property (nonatomic, strong) OrderEntity *orderEntity;

@end

@implementation ANOrderInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"订单详情";
    
    [self requestData];
}

- (void)requestData
{
    NSDictionary *body = @{@"id":self.orderId};
    [URLManager.shareManager getUrlStr:Api_Mine_Order_Detail parameters:body entityClass:[OrderEntity class] success:^(NSURLResponse * _Nonnull response, BaseEntity * _Nullable baseEntity, NSDictionary * _Nullable otherResponse) {
        if (baseEntity) {
            if (baseEntity.code.intValue == 200) {
                self.orderEntity = baseEntity.data;
                [self setupUIData];
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)requestEditState
{
    NSString *fieldValue = @"";
    if (self.orderEntity.state.intValue == 0) {
        fieldValue = @"4";
    }
    if (self.orderEntity.state.intValue == 2) {
        fieldValue = @"3";
    }
    NSDictionary *body = @{
                           @"id":self.orderEntity.orderId,
                           @"fieldName":@"state",
                           @"fieldValue":fieldValue,
                           };
    [URLManager.shareManager postUrlStr:Api_Order_State_Edit parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                [MBProgressHUD showSuccess:responseObj[@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)setupUIData
{
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.orderEntity.state.intValue == 0 || self.orderEntity.state.intValue == 2) {
            make.top.left.right.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-(kWidth(50)+kiPhoneXInsetBottom));
        }
        else {
            make.edges.equalTo(self.view);
        }
        
    }];
    
    self.orderStateBgView = [[UIImageView alloc] init];
    self.orderStateBgView.image = [UIImage imageNamed:@"d_bg"];
    [self.scrollView addSubview:self.orderStateBgView];
    [self.orderStateBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.height.equalTo(@(kWidth(69)));
        make.width.equalTo(@(kScreenW));
    }];
    
    self.orderStateLab = [[UILabel alloc] init];
    //    self.orderStateLab.text = @"已完成";
    [self.orderStateBgView addSubview:self.orderStateLab];
    self.orderStateLab.textColor = [UIColor whiteColor];
    self.orderStateLab.font = [UIFont systemFontOfSize:15];
    [self.orderStateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.orderStateBgView);
        make.left.equalTo(self.orderStateBgView).offset(17);
    }];
    
    
    self.addressView = [[MineAddressView alloc] init];
    self.addressView.canSelect = NO;
    [self.scrollView addSubview:self.addressView];
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderStateBgView.mas_bottom).offset(10);
        make.left.equalTo(self.scrollView).offset(15);
        make.right.equalTo(self.scrollView).offset(-15);
    }];
    
    self.addressView.layer.cornerRadius = 4;
    self.addressView.layer.masksToBounds = YES;
    
    self.orderCartView = [[OrderCartView alloc] init];
    self.orderCartView.type = OrderCartViewNormal;
    [self.scrollView addSubview:self.orderCartView];
    [self.orderCartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addressView.mas_bottom).offset(10);
        make.height.equalTo(@(kWidth(200)));
        make.left.right.equalTo(self.addressView);
    }];
    self.orderCartView.layer.cornerRadius = 4;
    self.orderCartView.layer.masksToBounds = YES;
    
    self.orderInfoView = [[OrderInfoView alloc] init];
    [self.scrollView addSubview:self.orderInfoView];
    [self.orderInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderCartView.mas_bottom).offset(10);
        make.left.right.equalTo(self.addressView);
        make.bottom.equalTo(self.scrollView).offset(-(10+kiPhoneXInsetBottom));
    }];
    self.orderInfoView.layer.cornerRadius = 4;
    self.orderInfoView.layer.masksToBounds = YES;
    
    
    self.orderCartView.shopNameLab.text = self.orderEntity.consumerName;
    self.orderCartView.tradeNameLab.text = self.orderEntity.goodsName;
    self.orderCartView.tradeDesLab.text = self.orderEntity.summary;
    self.orderCartView.tradeTimeLab.text = String(self.orderEntity.goodsParam);
    self.orderCartView.tradeNumLab.text = [NSString stringWithFormat:@"x%@",self.orderEntity.dealNum];
    self.orderCartView.price = self.orderEntity.price;
    self.orderCartView.memberPrice = self.orderCartView.memberPrice;
//    float price = self.orderEntity.deaAmount.floatValue * self.orderEntity.dealNum.floatValue;
    self.orderCartView.payPriceLab.text = [NSString stringWithFormat:@"￥%.2f",self.orderEntity.deaAmount.floatValue];
    self.orderCartView.payLab.text = [NSString stringWithFormat:@"共%@件，合计：",self.orderEntity.dealNum];
    [self.orderCartView.tradeIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERADDRS,self.orderEntity.filePath]] placeholderImage:PlaceHolderImage];
    
    self.orderInfoView.orderEntity = self.orderEntity;
    
    self.addressView.nameLab.text = self.orderEntity.receContact;
    self.addressView.telLab.text = self.orderEntity.receTel;
    self.addressView.addressLab.text = self.orderEntity.receAddress;
    
    //状态（0待付款，1待发货，2待收货，3已完成，4关闭）
    if (self.orderEntity.state.intValue == 0) {
        self.orderStateLab.text = @"待付款";
    }
    else if (self.orderEntity.state.intValue == 1) {
        self.orderStateLab.text = @"待发货";
    }
    else if (self.orderEntity.state.intValue == 2) {
        self.orderStateLab.text = @"待收货";
    }
    else if (self.orderEntity.state.intValue == 3) {
        self.orderStateLab.text = @"已完成";
    }
    else if (self.orderEntity.state.intValue == 4) {
        self.orderStateLab.text = @"关闭";
    }
    
    if (self.orderEntity.state.intValue == 0 || self.orderEntity.state.intValue == 2) {
        self.actionView = [[OrderInfoActionView alloc] init];
        [self.view addSubview:self.actionView];
        [self.actionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self.view);
            if (iPhoneX || iPhoneXMAX|| iPhoneXR) {
                make.height.equalTo(@(kWidth(50)+kiPhoneXInsetBottom));
            }
            else {
                make.height.equalTo(@(kWidth(50)+kiPhoneXInsetBottom));
            }
        }];
        self.actionView.type = self.orderEntity.state.intValue;
        WeakSelf
        self.actionView.payHandler = ^{
            ANOrderPayController *vc = [[ANOrderPayController alloc] init];
            vc.orderEntity = weakSelf.orderEntity;
            vc.type = ANOrderPayControllerTypeMineOrder;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        
        self.actionView.cancelHandler = ^{
            [weakSelf requestEditState];
        };
        
        self.actionView.sureHandler = ^{
            [weakSelf requestEditState];
        };
    }
}


- (void)setupSubViews
{
    
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
